Skip to content

Fix ICE and type mismatch with array type aliases and const generics#7638

Open
Dnreikronos wants to merge 5 commits into
FuelLabs:masterfrom
Dnreikronos:fix/type-alias-array-const-generics-ice
Open

Fix ICE and type mismatch with array type aliases and const generics#7638
Dnreikronos wants to merge 5 commits into
FuelLabs:masterfrom
Dnreikronos:fix/type-alias-array-const-generics-ice

Conversation

@Dnreikronos
Copy link
Copy Markdown
Contributor

@Dnreikronos Dnreikronos commented May 26, 2026

Description

Closes #7616

Using a type alias for an array (type ArrayU8Len2 = [u8; 2]) as a struct field and then comparing two such structs with == triggered an ICE: "Const generic not materialized". The Eq impl for arrays is generic over the length N, so resolving it needs the alias to carry its concrete length down into the const generic machinery. Two spots dropped it on the way.

extract_type_parameters now pulls the length out of array types the same way it already does for structs and enums: when the original side carries an ambiguous length variable (N) and the concrete side has a literal, N gets bound to that literal. MaterializeConstGenerics for TypeId also handles TypeInfo::Alias now, recursing into the aliased type instead of stopping at the alias, which mirrors the existing Ref handling.

The new e2e test exercises array aliases and nested aliases ([ArrayU8Len2; 3]) as struct fields, equality comparison to hit the Eq const generic path, and Vec<Alias>::get().unwrap() return-type resolution. Existing type_alias*, const_generic*, and array* suites still pass.

Checklist

  • I have linked to any relevant issues.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have updated the documentation where relevant (API docs, the reference, and the Sway book).
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added (or requested a maintainer to add) the necessary Breaking* or New Feature labels where relevant.
  • I have done my best to ensure that my PR adheres to the Fuel Labs Code Review Standards.
  • I have requested a review from the relevant team or maintainers.

extract_type_parameters ignored array lengths when matching concrete
arrays against generic impls like [T; N], so N was never mapped to its
literal value. Additionally, MaterializeConstGenerics did not recurse
through TypeInfo::Alias, stopping materialization at the alias boundary.

Closes FuelLabs#7616
Covers struct fields typed as array aliases, nested aliases, equality
via the Eq trait, and Vec<Alias>::get().unwrap() return type resolution.
@Dnreikronos Dnreikronos requested a review from a team as a code owner May 26, 2026 18:06
@fuel-cla-bot
Copy link
Copy Markdown

fuel-cla-bot Bot commented May 26, 2026

Thanks for the contribution! Before we can merge this, we need @Dnreikronos to sign the Fuel Labs Contributor License Agreement.

@cursor
Copy link
Copy Markdown

cursor Bot commented May 26, 2026

PR Summary

Medium Risk
Touches core type-system const-generic and unification paths in sway-core; scope is narrow but compiler correctness regressions are possible.

Overview
Fixes compiler crashes and wrong types when array length type aliases (e.g. type ArrayU8Len2 = [u8; 2]) appear in structs, ==, and generic APIs like Vec::get.

MaterializeConstGenerics for TypeId now handles TypeInfo::Alias by materializing through the aliased type and re-inserting the alias—same idea as the existing Ref path—so const generics behind aliases are not left unmaterialized.

extract_type_parameters for matching Array types now also records length const generics (ambiguous length name → concrete literal), alongside the existing element-type walk—so trait/Eq instantiation sees concrete lengths when aliases peel to [T; N].

Adds e2e type_alias_array_const_generics (nested aliases, struct fields, equality, Vec return typing).

Reviewed by Cursor Bugbot for commit 33126b7. Bugbot is set up for automated code reviews on this repo. Configure here.

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Jun 4, 2026

Merging this PR will not alter performance

✅ 25 untouched benchmarks


Comparing Dnreikronos:fix/type-alias-array-const-generics-ice (33126b7) with master (ac933ff)

Open in CodSpeed

@Dnreikronos
Copy link
Copy Markdown
Contributor Author

@ironcev this is ready. MaterializeConstGenerics now handles TypeInfo::Alias the same way it already does Ref, and extract_type_parameters records length const generics on Array matches. Mostly want to know if the alias-peeling approach is how you'd want it handled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ICE and wrong type mismatch error when using type aliases and traits implemented for const generics arrays

1 participant